/*
 * This file Copyright (c) 2015. Walle.
 * (http://www.wallellen.com). All rights reserved.
 *
 *
 * This file is dual-licensed under both the
 * Walle Agreement (WA) and the GNU General Public License.
 * You may elect to use one or the other of these licenses.
 *
 * This file is distributed in the hope that it will be
 * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
 * Redistribution, except as permitted by whichever of the GPL
 * or WA you select, is prohibited.
 *
 * 1. For the GPL license (GPL), you can redistribute and/or
 * modify this file under the terms of the GNU General
 * Public License, Version 3, as published by the Free Software
 * Foundation.  You should have received a copy of the GNU
 * General Public License, Version 3 along with this program;
 * if not, write to the Free Software Foundation, Inc., 51
 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * 2. For the Walle Agreement (WA), this file
 * and the accompanying materials are made available under the
 * terms of the WA which accompanies this distribution, and
 * is available at http://www.wallellen.com/agreement.html
 *
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 */

package com.wallellen.util.web;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by walle on 12/9/15.
 */
public class WebHotCase {
    private static List<HotCase> HOT_CASE = new ArrayList();
    private static Map<String, Integer> HOT_CASE_MAP = new HashMap();
    private static int maxNum = 1000;
    private static int minNum = 500;
    private static int index = 0;

    public static void putCase(String caseStr) {
        Integer num = (Integer) HOT_CASE_MAP.get(caseStr);
        if (num == null) {
            num = Integer.valueOf(0);
        }
        index += 1;
        HOT_CASE_MAP.put(caseStr, Integer.valueOf(num.intValue() + 1));
        if ((HOT_CASE_MAP.size() >= maxNum) || (index == 1) || (index == 5) || (index == 10) || (index % 20 == 0))
            clearHotCase();
    }

    public static List<String> getCases(int num) {
        List list = new ArrayList();
        for (int n = 0; (n < HOT_CASE.size()) && (n < num); n++) {
            list.add(((HotCase) HOT_CASE.get(n)).key);
        }
        return list;
    }

    private static void clearHotCase() {
        HOT_CASE.clear();

        for (String key : HOT_CASE_MAP.keySet()) {
            int num = ((Integer) HOT_CASE_MAP.get(key)).intValue();
            HOT_CASE.add(new HotCase(key, num));
        }

        Collections.sort(HOT_CASE, (o1, o2) -> o2.num - o1.num);
        if (HOT_CASE.size() >= minNum) {
            HOT_CASE = HOT_CASE.subList(0, minNum);
        }
        if (index >= minNum) {
            index = minNum;
        }

        HOT_CASE_MAP.clear();
        for (HotCase hc : HOT_CASE) {
            int cnum = hc.num;

            if (cnum > 0) {
                cnum /= 2;
            }
            HOT_CASE_MAP.put(hc.key, Integer.valueOf(cnum));
        }
    }
}
